package cn.sdormitory.evaluation.service.impl;

import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.common.utils.DataRoleUtil;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.evaluation.dao.WorkWeekDao;
import cn.sdormitory.evaluation.entity.WorkDayEntity;
import cn.sdormitory.evaluation.entity.WorkWeekEntity;
import cn.sdormitory.evaluation.service.WorkWeekService;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDept;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkWeekServiceImpl extends ServiceImpl<WorkWeekDao, WorkWeekEntity> implements WorkWeekService {
    @Resource
    DataRoleUtil dataRoleUtil;

    @Override
    public IPage<WorkWeekEntity> pageList(WorkWeekEntity vo) {
        LambdaQueryWrapper<WorkWeekEntity> wrapper = new LambdaQueryWrapper<>();
        //获取查询当前用户能查询到的范围
        List<String> userNameList = dataRoleUtil.getDeptSubordinateUserList();
        wrapper.in(userNameList.size()>0,WorkWeekEntity::getRegistrant,userNameList);

        wrapper.eq(!StringUtils.isEmpty(vo.getBeginDate()),WorkWeekEntity::getBeginDate,vo.getBeginDate());
        wrapper.orderBy(true,false,WorkWeekEntity::getCreatedTime);
        Page<WorkWeekEntity> page = page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
        return page;
    }

    @Override
    public int updateWeek(WorkWeekEntity vo) {
        if(StringUtils.isEmpty(vo.getClassMeeting())){
            throw new ApiException("周会总结不能为空");
        }
        vo.setScore("0");
        vo.setModifyBy(SysUserUtils.getSysUser().getUsername());
        vo.setModifyTime(new Date());
        int i = this.baseMapper.updateById(vo);
        return i;
    }
}
